<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: HCZ <huangchengze@etrol.com>
// +--------------------------------------
namespace app\index\controller;

use app\common\controller\Admin;
use app\index\controller\api\Index as indexApi;
class Index extends Admin
{
	protected $noNeedLogin = ['login','logout'];
	protected $api;
	
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new indexApi();
	}
	
	/**
	 * 系统首页
	 */
	public function index(){	
		$auditItemNum = $this->api->countAuditItem($this->scope)->getData();
		$this->assign('auditItemNum',$auditItemNum['data'][0]);
		$alarmNum = $this->api->countAlarm($this->scope)->getData();
		$this->assign('alarmNum',$alarmNum['data'][0]);
		$this->assign('scope',$this->scope);
		return $this->fetch(); 
    }
    
    /**
     * 系统登录
     */
    public function login(){
   		//判断是否已经登录
//     	if ($this->isLogin())
//     	{
//     		$this->error("你已经登录过了",'index/index');
//     	}
    	if(IS_POST){
    		$username = input('username');
    		$password = input('password');
    		$result = $this->auth->login($username, $password);
    		if($result['code'] == 1){
                setlog('login','',session('user_auth.id'));
    			$this->success($result['msg'],'index/index',$result['data']);
    		}else{
    			$this->error($result['msg']);
    		}
    	}
    	return $this->fetch();   	
    }
    
    public function logout() {
    	$this->auth->logout();
    	return $this->success('注销成功', 'index/login');
    }
    
    public function test() {
    	return $this->fetch();
    }
}
